/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.controller;

import com.wisnu.spms.source.App;
import com.wisnu.spms.source.Employee;
import com.wisnu.spms.source.model.MainFrameModel;
import com.wisnu.spms.source.view.MainFrame;
import com.wisnu.spms.source.view.PanelAdmin;
import com.wisnu.spms.source.view.PanelLogin;
import com.wisnu.spms.source.view.PanelPayment;
import com.wisnu.spms.source.view.PanelStudent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Wisnu Wardoyo
 */
public class MainFrameController {

    private final MainFrame frame;
    private final MainFrameModel model;

    public MainFrameController(MainFrame frame, MainFrameModel model) {
        this.frame = frame;
        this.model = model;
        initComponents();
    }

    public final void initComponents() {
        try {
            if (model.loginCheck()) {
                showAdminPanel();
            } else {
                login();
            }
        } catch (Exception ex) {

        }
    }

    public void searchId() {
        if (Employee.getEmployee() != null) {
            if (App.getPage() != 1001) {
                String stringId = frame.getTxtSearchId().getText();
                try {
                    int id = Integer.parseInt(stringId);
                    PanelPayment panelPayment = new PanelPayment(id);

                    frame.getScrollPaneContent().setViewportView(panelPayment);

                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(frame, "ID Tidak Valid");
                }
            } else {
                String keyword = frame.getTxtSearchId().getText();
                try {
                    int id = Integer.parseInt(keyword);
                    PanelStudent panelStudent = new PanelStudent(id);
                    
                    frame.getScrollPaneContent().setViewportView(panelStudent);
                } catch (NumberFormatException e) {
                    PanelStudent panelStudent = new PanelStudent(keyword);
                    
                    frame.getScrollPaneContent().setViewportView(panelStudent);
                }
            }
        } else {
            JOptionPane.showMessageDialog(frame, "Anda Harus Login Terlebih Dahulu");
        }
    }

    public void login() {
        PanelLogin panelLogin = new PanelLogin(this);
        frame.getScrollPaneContent().setViewportView(panelLogin);
        App.setPage(1);
    }

    public void showAdminPanel() {
        PanelAdmin panelAdmin = new PanelAdmin();
        frame.getScrollPaneContent().setViewportView(panelAdmin);
        App.setPage(1000);
    }

    public void logout() {
        if (Employee.getEmployee() != null) {
            try {
                model.logout();
                login();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(frame, "Tidak Bisa Logout!");
            }
        }
    }

    public void test() {
        PanelStudent panelStudent = new PanelStudent();
        frame.getScrollPaneContent().setViewportView(panelStudent);
        App.setPage(1001);
    }

}
